package SEApp.dialogs
{
	import SEApp.boxes.albums.se_albums;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import UIUtils.uif_util;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class se_albumsDialog extends se_dialog {
		private var _albums: se_albums;
		public static const NOTIFY_PHOTOSELECTED: String = 'NOTIFY_PHOTOSELECTED';
		
		public function se_albumsDialog(parent:uif_component, asModal:Boolean=false, canClose:int=1) {
			super(parent, 0, 0, 650, 260, asModal, canClose);
			initChilds(this, getInner());
		}
		
		protected function initChilds(contentLayer: uif_ecomponent, inner: Rectangle):void {
			title.text = 'Альбомы';
			var inner: Rectangle = contentLayer.getInner();
			seApp.externalServer.getAlbums(seApp.externalServer.viewer_id, function(data: Array): void {
				_albums = new se_albums(contentLayer, seApp.externalServer.viewer_id, data, 6, true, inner.x, 
										inner.y, inner.width, inner.height, inner.width, style['albums']);
			});
		}
		
		public function get albums(): se_albums {
			return _albums;
		}
		
		public function selectAlbum(): void {
			function doRemoveEvents(e: Event): void {
				removeEventListener(Event.CLOSE, doRemoveEvents);
				_albums.removeEventListener(Event.SELECT, doSelectPhoto);
			}
			
			function doSelectPhoto(e: ObjectEvent): void {
				doRemoveEvents(e);
				dispatchEvent(new ObjectEvent(NOTIFY_PHOTOSELECTED, e.object.data, true));
				close();
			}
			
			show();
			uif_util.waitCall(function(): Boolean {
				return _albums != null
			}, function(): void {
				addEvent(Event.CLOSE, doRemoveEvents);
				_albums.addEvent(Event.SELECT, doSelectPhoto);
			});
		}
		
	}
}